package edu.uit.se.laps.web.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import edu.uit.se.laps.web.model.CTDonHang;
import edu.uit.se.laps.web.model.DonHang;
import edu.uit.se.laps.web.model.LoaiSanPham;
import edu.uit.se.laps.web.model.SanPham;
import edu.uit.se.laps.web.model.TaiKhoan;

public class ShoppingCart implements Serializable {
	/**
	 * Quan ly cac san pham trong gio hang
	 */
	private static final long serialVersionUID = 5070252388675772724L;
	
	 

	private List<SanPham> spList;
	private List<CTDonHang> cTDHList;
	//private List<DonHang> dhList;
	
	private CTDonHangManager cTDHManager;
	private DonHangManager dhManager;
	
	//Quan ly tai khoan admin, user
	private String email;
	private String password;
	
	
	//lay danh sach loai san pham
	private LoaiSanPhamManager loaiSpManager;
	private SanPhamManager spManager;
	
	//bien tam dung trong update san pham
	private SanPham spTemp;
	
	//bien tam dung trong update loai san pham
	private LoaiSanPham loaiSPTemp;
	
	//bien tam dung trong lay thong tin khach hang khi checkout
	private TaiKhoanManager tkManager;
	
	//bien tam dung trong update don hang
	private DonHang dhTemp;
	
	public ShoppingCart() {

		spList = new ArrayList<>();
		cTDHList = new ArrayList<>();
		
		dhManager = new DonHangManager();
		cTDHManager = new CTDonHangManager();
		
		loaiSpManager = new LoaiSanPhamManager();
		spManager = new SanPhamManager();
		
		tkManager = new TaiKhoanManager();
	}

	public List<SanPham> getSPList() {
		return spList;
	}

	public void addSPList(SanPham newSP) {

		spList.add(newSP);
		
	}

	//Them san pham vao gio hang
	public void addCTDHList(CTDonHang newCTDH) {
		boolean flag = false;
		// cTDHList.add(newCTDH);
		if (cTDHList.size() == 0) {
			cTDHList.add(newCTDH);
			return;
		}

		//co cung 1 san pham thi tang so luong len
		for (int i = 0; i < cTDHList.size(); i++) {
			CTDonHang ctdh = (CTDonHang) cTDHList.get(i);
			if (ctdh.getIdSP() == newCTDH.getIdSP()) {
				ctdh.setSoLuong(ctdh.getSoLuong() + newCTDH.getSoLuong());
				ctdh.setThanhTien(ctdh.getDonGia() * ctdh.getSoLuong());
				cTDHList.set(i, ctdh);
				flag = true;
				break;
			}
		}
		if ((newCTDH.getSoLuong() > 0) && (flag == false)) {
			cTDHList.add(newCTDH);
		}

	}
	
	//xoa san pham tu gio hang
	public void deleteItem(int itemIndex) {
		// items.removeElementAt(itemIndex);
		boolean flag = false;
		int index = -1;
		for (int i = 0; i < cTDHList.size(); i++){
			CTDonHang ctdh = (CTDonHang) cTDHList.get(i);
			if (ctdh.getIdSP() == itemIndex){
				flag = true;
				index = i;
			}
		}
		if (flag == true)
			cTDHList.remove(index);
	}

	//thanh toan gio hang
	public float checkOut(String email){
		
		TaiKhoan tk = tkManager.getUser(email);
		
		float tongTien = 0;
		int idDH = 0;
		if (cTDHList.size() == 0){
			return 0;
		}
		//tinh tong tien thanh toan trong don hang
		for (int i = 0; i < cTDHList.size(); i++){
			CTDonHang ctdh = (CTDonHang) cTDHList.get(i);
			tongTien += ctdh.getThanhTien();
		}
		//khoi tao don hang
		DonHang dh = new DonHang();
		dh.setIdUser(tk.getIdUser());
		dh.setTenNguoiNhan(tk.getHoTen());
		dh.setDiaDiemGiaoHang(tk.getDiaChi());
		
		dh.setTongTien(tongTien);
		dh.setTinhTrang("Đang giao");
		dh.setGhiChu("");
		
		dhManager.addDonHang(dh);
		
		//lay thong tin id cua don hang
		idDH = dh.getIdDH();
		
		//khoi tao cac chi tiet don hang
		for (int i = 0; i < cTDHList.size(); i++){
			CTDonHang ctdh = (CTDonHang) cTDHList.get(i);
			ctdh.setIdDH(idDH);
			cTDHManager.addCTDonHang(ctdh);
		}
		
		//xoa list ct don hang
		cTDHList.clear();
		return tongTien;
	}
	
	
	public List<CTDonHang> getcTDHList() {
		return cTDHList;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public List<LoaiSanPham> getLoaiSP() {
		return loaiSpManager.list();
	}
	
	public List<SanPham> getspList(){
		return spManager.list();
	}
		 
	public List<SanPham> getspTypeList(int loaiSP){
		return spManager.listSp(loaiSP);
	}
	 

	public SanPham getSpTemp() {
		return spTemp;
	}

	public void setSpTemp(SanPham spTemp) {
		this.spTemp = spTemp;
	}

	public LoaiSanPham getLoaiSPTemp() {
		return loaiSPTemp;
	}

	public void setLoaiSPTemp(LoaiSanPham loaiSPTemp) {
		this.loaiSPTemp = loaiSPTemp;
	}

	public List<DonHang> getDHList(){
		return dhManager.list();
	}

	public DonHang getDhTemp() {
		return dhTemp;
	}

	public void setDhTemp(DonHang dhTemp) {
		this.dhTemp = dhTemp;
	}
	
	
}
